package com.hoyue.dao.impl;

import com.hoyue.bean.BackPicture;
import com.hoyue.dao.BackPicDao;
import com.hoyue.service.BackPicService;
import com.hoyue.utils.C3P0Utils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;
import java.util.List;

/**
 * @author Hoyue_network1
 * @version 1.0
 * Creat by 2018/8/22 23:42
 */
public class BackPicDaoImpl implements BackPicDao{
	// 获取queryrunner对象
	QueryRunner qr = C3P0Utils.getQueryRunner();

	public List<BackPicture> getBackPicture() throws SQLException {

		String sql = "select * from front_page";

		List<BackPicture> list = qr.query(sql, new BeanListHandler<>(BackPicture.class));

		return list;
	}

	@Override
	public void updatePicture(BackPicture backPicture) throws SQLException {
		String sql = "UPDATE front_page SET address=?, title=?, description=? WHERE id=?";

		qr.update(sql, backPicture.getAddress(), backPicture.getTitle(), backPicture.getDescription(),backPicture.getId());
	}

	@Override
	public String getAddressById(int id) throws SQLException {
		String sql = "SELECT address FROM front_page WHERE id=?";

		return qr.query(sql,new ScalarHandler<>(),id);
	}
}
